package com.demon.auto.handle;

import com.demon.auto.model.ApiResponseModel;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * 签到客户端
 *
 * @version 1.0
 * @author huangrz
 * @date 2025/03/18 11:50
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ApiResponseModel<?> handleValidationException(MethodArgumentNotValidException ex) {
        StringBuilder msg = new StringBuilder();
        ex.getBindingResult().getFieldErrors().forEach(fieldError ->
                // [field]：msg;
                msg.append("[").append(fieldError.getField()).append("]：")
                        .append(fieldError.getDefaultMessage()).append(";")
        );
        return ApiResponseModel.paramError(msg.toString());
    }

}